package com.wxy.controller;

import com.wxy.pojo.User;
import com.wxy.service.UserService;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zzzzz
 * @create 09-11
 */
@Controller
public class UserController {

    @Resource
    UserService userServiceImpl;

    @RequestMapping("user/login")
    public ModelAndView login(User user, HttpSession session){
        ModelAndView view = new ModelAndView();
        User u = userServiceImpl.selectUserInfo(user);
        if(u == null){
            // return "redirect:/error"
            return new ModelAndView("redirect:/error");
        }
        session.setAttribute("loginusername",u.getUsername());
        return new ModelAndView("redirect:/main");
    }

    @RequestMapping("user/register")
    public String register(User user, HttpSession session){
        User u = userServiceImpl.selectUserInfoByname(user.getUsername());
        if(u != null){
            return "redirect:/error";
        }
        SimpleDateFormat format = new SimpleDateFormat("YYYY:MM:dd HH:mm:SS");
        Date date = new Date();
        String time = format.format(date);
        user.setRegdate(date);
        System.out.println(user);
        int index = userServiceImpl.insertUserInfo(user);
        if(index == 0){
            return "/error";
        }
        session.setAttribute("loginusername",user.getUsername());
        return "redirect:/main";
    }

    @RequestMapping("userManager/add")
    public String userInfoAdd(User user, @RequestParam("file_img") MultipartFile files){
        String userImagesUrl = "files\\head\\";
        String serverFilesPath = "src\\main\\webapp\\files\\head\\";

        //获取项目的路径
        String rootPath = System.getProperty("webapp.root");
        int index = rootPath.indexOf("target");
        String sub = rootPath.substring(0, index);
        //拼接文件存放路径 + 文件名
        String filePath = sub + serverFilesPath + files.getOriginalFilename();
        //拼接数据库中存放的路径
        userImagesUrl = userImagesUrl + files.getOriginalFilename();
        try {
            //获取文件流 处理异常
            InputStream is = files.getInputStream();
            //使用工具类将文件存储到本地
            FileUtils.copyInputStreamToFile(is,new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置当前用户的头像路径
        user.setImages(userImagesUrl);
        System.out.println(user);
        System.out.println(filePath);
        //存储信息进数据库
        int i = userServiceImpl.insertUserInfo(user);
        return "userManager/showUserInfo.jsp";
    }
}
